package com.easy.controller;

import com.easy.bean.Customer;
import com.easy.service.CustomerService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin // 允许跨域
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @PostMapping
    public Result add(@RequestBody Customer customer) {
        Object obj = customerService.add(customer);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(@RequestBody Customer customer) {
        Object obj = customerService.edit(customer);
        return Result.success(obj);
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id) {
        Object obj = customerService.del(id);
        return Result.success(obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id) {
        Object obj = customerService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(Customer customer) {
        Object obj = customerService.all(customer);
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page(Page page, Customer customer) {
        // 查询一共有多少条数据
        // 当前页要显示的数据
        // 将以上数据封装到page对象中
        // 返回page对象
        customerService.page(page, customer);
        // 返回page对象
        return Result.success(page);
    }
    @GetMapping("getByPhone")
    public Result getByPhone(@RequestParam("phone") String phone) {
        Object obj = customerService.getByPhone(phone);
        return Result.success(obj);
    }

}
